package com.yaodian100.ec.membergift.web;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.memberevent.entity.MemberGift;
import com.yaodian100.ec.memberevent.service.MemberEventService;

/**
 * 加入会员送赠品
 * 
 * @author apple.chang
 * 
 */
public class MemberGiftAction extends ActionSupport implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -527250966907221685L;

	private MemberEventService memberEventService;
	private DomainService<Menu> menuService;
	private SimplePager pager = null;
	private List<MemberGift> mgList;
	private Menu mgOnTypeMenu;
	private Menu mgTypeMenu;
	private Menu channelNameMenu;

	/******* 查询页面参数 ********/
	private String nameParam;
	private String startDateParam;
	private String endDateParam;
	private String minDate;
	private String mgOnTypeParam;
	private String mgTypeParam;
	private String isActivateParam;

	/******* 编辑页面参数 ********/
	private String id;
	private String name;
	private String asname;
	private String startDate;
	private String endDate;
	private String eminDate;
	private String startTime;
	private String endTime;
	private String mgType; // 1:赠品 2:特惠商品 3:站内活动
	private String action_result;
	private String memo;
	private String prodId;
	private String redirect_url;
	private String isActivate;

	public String search() throws ParseException {
		minDate = DateUtil.convertDateToString("yyyy,MM - 1,dd", DateUtil.getTodayAddDays(-60).getTime());
		// search value
		CommonCriteria crit = new CommonCriteria();
		if (StringUtils.isNotBlank(nameParam))
			crit.addRlike("applySource", nameParam);
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		if (StringUtils.isNotBlank(startDateParam)) {
			Date sday = dateFormat.parse(startDateParam);
			crit.addGe("startDate", sday);
		}
		if (StringUtils.isNotBlank(endDateParam)) {
			Date eday = dateFormat.parse(endDateParam);
			crit.addLe("endDate", eday);
		}
		if (StringUtils.isNotBlank(mgOnTypeParam)) {
			Date now = new Date();
			if (mgOnTypeParam.equals("WAIT")) {
				crit.addGe("startDate", now);
			} else if (mgOnTypeParam.equals("ON")) {
				crit.addLe("startDate", now);
				crit.addGe("endDate", now);
			} else if (mgOnTypeParam.equals("OVERDUE")) {
				crit.addLe("endDate", now);
			}
		}
		if (StringUtils.isNotBlank(mgTypeParam)) {
			if (mgTypeParam.equals("GIFT")) {
				crit.addEq("type", "1");
			} else if (mgTypeParam.equals("REDIRECT")) {
				crit.addEq("type", "3");
			}
		}
		if (StringUtils.isNotBlank(isActivateParam)) {
			crit.addEq("is_activate", isActivateParam);
		}

		int contentSize = memberEventService.getMemberGiftListSize(crit).intValue();
		pager.setPageRecord(20);
		pager.setTotalSize(contentSize);
		mgList = memberEventService.getMemberGiftList(pager.getCurrentPage() * pager.getPageRecord(),
				pager.getPageRecord(), crit, new String[] { "createDate desc" });
		return "list";
	}

	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		if (null == pager)
			pager = new SimplePager();
		if (null == mgOnTypeMenu)
			mgOnTypeMenu = menuService.get("MemberEvent.OnType");
		if (null == mgTypeMenu)
			mgTypeMenu = menuService.get("MemberGift.Type");
		if (null == channelNameMenu)
			channelNameMenu = menuService.get("Channel.Name");
	}

	/**
	 * 新增
	 * 
	 * @return
	 */
	public String add() {
		asname = "";
		name = "";
		eminDate = DateUtil.convertDateToString("yyyy,MM - 1,dd", new Date());
		DateFormat dateFormat = new SimpleDateFormat(EcConstant.PATTERN_DATE);
		startDate = dateFormat.format(new Date());
		endDate = dateFormat.format(DateUtils.addDays(new Date(), 7));
		startTime = "00:00";
		endTime = "23:59";
		id = "";
		mgType = "1";
		prodId = null;
		redirect_url = null;
		memo = null;
		return "edit";
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	public String edit() {
		MemberGift mg = memberEventService.getMemberGiftbyId(id);
		asname = mg.getApplySource();
		name = mg.getName();
		Date tmp = new Date();
		if (mg.getStartDate().getTime() < System.currentTimeMillis()) {
			tmp = mg.getStartDate();
		}
		eminDate = DateUtil.convertDateToString("yyyy,MM - 1,dd", tmp);
		DateFormat dateFormat = new SimpleDateFormat(EcConstant.PATTERN_DATE);
		startDate = dateFormat.format(mg.getStartDate());
		endDate = dateFormat.format(mg.getEndDate());
		dateFormat = new SimpleDateFormat("HH:mm");
		startTime = dateFormat.format(mg.getStartDate());
		endTime = dateFormat.format(mg.getEndDate());
		mgType = mg.getType();
		memo = mg.getMemo();
		if (mgType.equals("1"))
			prodId = mg.getContent();
		else
			redirect_url = mg.getContent();
		return "edit";
	}

	public String save() throws ParseException {
		MemberGift mg;
		if (StringUtils.isNotBlank(id)) {
			mg = memberEventService.getMemberGiftbyId(id);
		} else {
			mg = new MemberGift();
		}
		mg.setName(name);
		mg.setApplySource(asname);
		DateFormat dateFormat = new SimpleDateFormat(EcConstant.PATTERN_TIMESTAMP_HH_MM);
		Date sday = dateFormat.parse(startDate + " " + startTime);
		Date eday = dateFormat.parse(endDate + " " + endTime);
		mg.setStartDate(sday);
		mg.setEndDate(eday);
		mg.setType(mgType);
		mg.setMemo(memo);
		mg.setIs_activate("Y");
		if (mg.getType().equals("1")) {
			mg.setContent(prodId);
		} else if (mg.getType().equals("3")) {
			mg.setContent(redirect_url);
		}
		if (StringUtils.isNotBlank(id)) {
			if (null == memberEventService.saveMemberGift(mg))
				return "edit";
		} else {
			if (null == memberEventService.saveMemberGift(mg))
				return "add";
		}
		return "save_ok";
	}

	public MemberEventService getMemberEventService() {
		return memberEventService;
	}

	public void setMemberEventService(MemberEventService memberEventService) {
		this.memberEventService = memberEventService;
	}

	public SimplePager getPager() {
		return pager;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public Menu getMgOnTypeMenu() {
		return mgOnTypeMenu;
	}

	public void setMgOnTypeMenu(Menu mgOnTypeMenu) {
		this.mgOnTypeMenu = mgOnTypeMenu;
	}

	public DomainService<Menu> getMenuService() {
		return menuService;
	}

	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public List<MemberGift> getMgList() {
		return mgList;
	}

	public void setMgList(List<MemberGift> mgList) {
		this.mgList = mgList;
	}

	public String getNameParam() {
		return nameParam;
	}

	public void setNameParam(String nameParam) {
		this.nameParam = nameParam;
	}

	public String getStartDateParam() {
		return startDateParam;
	}

	public void setStartDateParam(String startDateParam) {
		this.startDateParam = startDateParam;
	}

	public String getEndDateParam() {
		return endDateParam;
	}

	public void setEndDateParam(String endDateParam) {
		this.endDateParam = endDateParam;
	}

	public String getMinDate() {
		return minDate;
	}

	public void setMinDate(String minDate) {
		this.minDate = minDate;
	}

	public String getMgOnTypeParam() {
		return mgOnTypeParam;
	}

	public void setMgOnTypeParam(String mgOnTypeParam) {
		this.mgOnTypeParam = mgOnTypeParam;
	}

	public String getMgTypeParam() {
		return mgTypeParam;
	}

	public void setMgTypeParam(String mgTypeParam) {
		this.mgTypeParam = mgTypeParam;
	}

	public String getIsActivateParam() {
		return isActivateParam;
	}

	public void setIsActivateParam(String isActivateParam) {
		this.isActivateParam = isActivateParam;
	}

	public String getAsname() {
		return asname;
	}

	public void setAsname(String asname) {
		this.asname = asname;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getEminDate() {
		return eminDate;
	}

	public void setEminDate(String eminDate) {
		this.eminDate = eminDate;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getMgType() {
		return mgType;
	}

	public void setMgType(String mgType) {
		this.mgType = mgType;
	}

	public String getAction_result() {
		return action_result;
	}

	public void setAction_result(String actionResult) {
		action_result = actionResult;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public String getIsActivate() {
		return isActivate;
	}

	public void setIsActivate(String isActivate) {
		this.isActivate = isActivate;
	}

	public Menu getMgTypeMenu() {
		return mgTypeMenu;
	}

	public void setMgTypeMenu(Menu mgTypeMenu) {
		this.mgTypeMenu = mgTypeMenu;
	}

	public String getProdId() {
		return prodId;
	}

	public void setProdId(String prodId) {
		this.prodId = prodId;
	}

	public String getRedirect_url() {
		return redirect_url;
	}

	public void setRedirect_url(String redirectUrl) {
		redirect_url = redirectUrl;
	}

	public Menu getChannelNameMenu() {
		return channelNameMenu;
	}

	public void setChannelNameMenu(Menu channelNameMenu) {
		this.channelNameMenu = channelNameMenu;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}